package com.ciban.smarthome.module.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ciban.smarthome.common.utils.RegexUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.db.DbBaseDao;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.utils.ModuleUtils;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/5.
 */
public class AreaDao extends DbBaseDao {
    public AreaDao(Context context) {
        super(context, "area");
    }

    /*_id integer PRIMARY KEY autoincrement, name varchar(30),icon_path varchar(50),host_id integer,equip_list varchar(50)*/

    /**
     * 添加新区域
     *
     * @param info
     * @return
     */
    public long addNewArea(Area info) {
        ContentValues values = new ContentValues();
        values.put("_id", ModuleUtils.getIDByRule());
        values.put("name", info.getName());
        values.put("icon_path", info.getIcon_path());
        values.put("equip_list", info.getEquip_list());
        values.put("host_id", info.getHost_id());

        return insert(null, values);
    }

    /**
     * 通过区域id 获取区域的详细信息
     *
     * @param id
     * @return
     */
    public Area getAreaDetails(String id) {
        Cursor cursor = query(new String[]{"name", "icon_path", "host_id", "equip_list", "_id"}, "_id=?", new String[]{id}, null);
        Area info = null;
        if (cursor != null && cursor.moveToNext()) {
            info = new Area();
            info.setName(cursor.getString(0));
            info.setIcon_path(cursor.getString(1));
            info.setHost_id(cursor.getString(2));
            info.setEquip_list(cursor.getString(3));
            info.setId(cursor.getString(4));
        }
        return info;
    }

    /**
     * 获取所有区域列表
     *
     * @return
     */
    public ArrayList<Area> getAreaList() {
        ArrayList<Area> list = new ArrayList<Area>();
        Cursor cursor = query(new String[]{"name", "icon_path", "host_id", "equip_list", "_id"}, null, null, null);
        Area info = null;
        while (cursor.moveToNext()) {
            info = new Area();
            info.setName(cursor.getString(0));
            info.setIcon_path(cursor.getString(1));
            info.setHost_id(cursor.getString(2));
            info.setEquip_list(cursor.getString(3));
            info.setId(cursor.getString(4));

            list.add(info);
        }
        return list;
    }

    /**
     * 根据区域id 删除某个区域
     *
     * @param id
     * @return
     */
    public int delArea(String id) {
        return del("_id=?", new String[]{id});
    }

    /**
     * 更新区域信息
     *
     * @param info
     * @return
     */
    public int updateArea(Area info) {
        ContentValues values = new ContentValues();
        values.put("name", info.getName());
        values.put("icon_path", info.getIcon_path());
        values.put("equip_list", info.getEquip_list());
        values.put("host_id", info.getHost_id());

        return update(values, "_id=?", new String[]{info.getId()});
    }

    /**
     * 添加区域下边对应的区域类型
     *
     * @param type
     */
    public boolean addEquipType(int type, String id) {
        String equip_list = getEquipTypeList(id);
        if(equip_list==null)
            equip_list="";
        LogUtil.d(AreaDao.class, "设备类型列表：" + equip_list);
        if (checkTypeExist(equip_list, type)) {
            LogUtil.d(AreaDao.class, "该设备类型已经存在");
            return true;
        }
        equip_list +=  ","+type ;
        ContentValues values = new ContentValues();
        values.put("equip_list", equip_list);
        LogUtil.d(AreaDao.class, "添加后的设备类型列表：" + equip_list);
        return update(values, "_id=?", new String[]{id}) > 0;
    }

    /**
     * 删除某个区域下对应的设备类型
     *
     * @param type
     * @param id
     * @return
     */
    public boolean delEquipType(int type, String id) {
        String equip_list = getEquipTypeList(id);
        if (!checkTypeExist(equip_list, type))
            return true;
        String[] split = equip_list.split(",");
        String info = "";
        equip_list = "";
        for (int i = 0; i < split.length; i++) {
            info = split[i];
            if (RegexUtils.isNumStr(info)) {
                if (type != Integer.parseInt(info))
                    equip_list += info + ",";
            }
        }
        ContentValues values = new ContentValues();
        values.put("equip_list", equip_list);
        return update(values, "_id=?", new String[]{id}) > 0;
    }

    public String getEquipTypeList(String id) {
        Cursor cursor = query(new String[]{"equip_list"}, "_id=?", new String[]{id}, null);
        String equip_list = null;
        if (cursor != null && cursor.moveToNext()) {
            equip_list = cursor.getString(0);
        }
        cursor.close();
        cursor = null;
        return equip_list;
    }

    /**
     * 检查电器类型是否存在
     */
    private boolean checkTypeExist(String equip_list, int type) {
        boolean result = false;
        if (equip_list != null) {
            String[] split = equip_list.split(",");
            for (int i = 0; i < split.length; i++) {
                if (RegexUtils.isNumStr(split[i]) && type == Integer.parseInt(split[i])) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

}
